import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';

const app = getApp();

Page({
    data: {
        member: 0
    },
    async onReady() {
        const db = qq.cloud.database();
        const _ = db.command;
        let This = this;
        await db.collection("act")
            .where({
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                This.setData({ member: res.data[0] });
            });
    },
    onShareAppMessage() {
        qq.showShareMenu({
            showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
        });
    },
    async recover(e) {
        let actNumb = this.data.member.act.indexOf(e.currentTarget.dataset.act);

        const db = qq.cloud.database();
        const _ = db.command;
        let admin = false;
        console.log(this.data.member);
        await db.collection('administer').where({
            unionId: _.eq(app.data.unionId)
        }).get().then((res) => {
            if (res.data.length != 0) {
                admin = true
            }
        })
        if (admin) {
            await db.collection('sign-in').where({
                [e.currentTarget.dataset.act]: _.exists(true),
                alliance: app.data.alliance
            }).get().then((res) => {
                if (res.data.length != 0) {
                    Dialog.confirm({
                            title: '提示',
                            message: '该行动含有其他成员的签到记录，请确认是否删除',
                        })
                        .then(() => {
                            let member = this.data.member;
                            member.act.splice(actNumb, 1);
                            member.antistop.splice(actNumb, 1);
                            member.timestamp.splice(actNumb, 1);
                            db.collection('act')
                                .where({
                                    alliance: app.data.alliance
                                }).update({
                                    data: {
                                        act: member.act,
                                        antistop: member.antistop,
                                        timestamp: member.timestamp
                                    }
                                }).then((res) => {
                                    db.collection("act")
                                        .where({
                                            alliance: app.data.alliance
                                        })
                                        .get()
                                        .then(res => {
                                            this.setData({ member: res.data[0] });
                                            console.log(res);
                                        }).catch(err => {
                                            console.log(err);
                                        })
                                })
                        })
                        .catch(() => {});
                } else {
                    let member = this.data.member;
                    member.act.splice(actNumb, 1);
                    member.antistop.splice(actNumb, 1);
                    member.timestamp.splice(actNumb, 1);
                    db.collection('act')
                        .where({
                            alliance: app.data.alliance
                        }).update({
                            data: {
                                act: member.act,
                                antistop: member.antistop,
                                timestamp: member.timestamp
                            }
                        }).then((res) => {
                            db.collection("act")
                                .where({
                                    alliance: app.data.alliance
                                })
                                .get()
                                .then(res => {
                                    this.setData({ member: res.data[0] });
                                }).catch(err => {
                                    console.log(err);
                                })
                        })

                }
            }).catch(err => {
                console.log(err);
            })
        } else {
            Toast('没有管理员权限');
        }


    }


})